package cn.duli.myvideo.controller;

import cn.duli.myvideo.pojo.Variety;
import cn.duli.myvideo.service.VarietyService;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageHelper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@CrossOrigin
@RestController
@RequestMapping("/variety")
public class VarietyController {

    @Resource
    private VarietyService varietyService;

    /**
     * 根据综艺名称模糊分页查询综艺列表
     * @return
     */
    @GetMapping("/queryLike")
    public PageInfo<Variety> queryLike(
            @RequestParam(value = "vName") String vName,
            @RequestParam(defaultValue = "1") int pageNo,
            @RequestParam(defaultValue = "2") int pageSize) {
        PageHelper.startPage(pageNo,pageSize);
        PageInfo<Variety> pageInfo = new PageInfo<>(varietyService.queryLike(vName));
        return pageInfo;
    }


    /**
     * 分页查看所有综艺列表
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/findAll")
    public PageInfo<Variety> findAll(
            @RequestParam(defaultValue = "1") int pageNo,
            @RequestParam(defaultValue = "2") int pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        PageInfo<Variety> pageInfo = new PageInfo<>(varietyService.findAll());
        return pageInfo;
    }


    /**
     * 根据id查找综艺
     * @param vId
     * @return
     */
    @GetMapping("/findById/{vId}")
    public Variety findById(@PathVariable("vId") Integer vId){
        return varietyService.findById(vId);
    }

    /**
     * 新增综艺
     * @param variety
     */
    @PostMapping("/save")
    public void save(@RequestBody Variety variety){
        varietyService.save(variety);
    }

    /**
     * 修改综艺
     * @param variety
     */
    @RequestMapping("/update")
    public void update(@RequestBody Variety variety){
        varietyService.update(variety);
    }

    /**
     * 根据id删除综艺
     * @param vId
     */
    @RequestMapping("/deleteById/{vId}")
    public void deleteById(@PathVariable("vId") Integer vId){
        varietyService.deleteById(vId);
    }

}
